package com.fenye.testflow.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fenye.testflow.model.common.Result;
import com.fenye.testflow.model.common.ResultCodeEnum;
import com.fenye.testflow.model.entity.testflow.Project;
import com.fenye.testflow.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/project")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    /**
     *  @author: fenye
     *  @Date:
     *  @Description: 新增
     */
    @PostMapping("save")
    public Result save(@RequestBody Project project) {
        boolean isSave = projectService.save(project);
        if (isSave) {
            return Result.build(null, ResultCodeEnum.SUCCESS);
        }
        return Result.build(null,ResultCodeEnum.DATA_ERROR);
    }

    @PostMapping("update")
    public Result update(@RequestBody Project project) {
        boolean isUpdate = projectService.updateById(project);
        if (isUpdate) {
            return Result.build(null, ResultCodeEnum.SUCCESS);
        }
        return Result.build(null,ResultCodeEnum.DATA_ERROR);
    }

    @DeleteMapping("/delete/{projectId}")
    public Result delete(@PathVariable Long projectId) {
        boolean isDelete = projectService.removeById(projectId);
        if (isDelete) {
            return Result.build(null, ResultCodeEnum.SUCCESS);
        }
        return Result.build(null,ResultCodeEnum.DATA_ERROR);
    }

    @GetMapping("/findById/{projectId}")
    public Result findByid(@RequestParam Long projectId) {
        Project project = projectService.getById(projectId);
        if (project == null) {
            return Result.build(null,ResultCodeEnum.DATA_ERROR);
        }
        return Result.build(project, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("findByPage/{page}/{size}")
    public Result findByPage(Project project,
            @PathVariable int page,
            @PathVariable int size) {

        Page<Project> pageParam = new Page<>(page, size);
        Page<Project> pageData = projectService.page(pageParam);
        return Result.build(pageData, ResultCodeEnum.SUCCESS);
    }

}
